#ifndef FLAT2008
#define FLAT2008

#include "Renderable.h"
#include "../GL/glut.h"

class FlatObject : public Renderable
{
	GLubyte *_bytes, *_picBytes;
	int _startX;
	int _startY;
	int _X;
	int _Y;
	int _width;
	int _height;
	bool _selected;

	void SetOrtho()
	{
		GLint viewport[4];
		glGetIntegerv(GL_VIEWPORT, viewport);

		glMatrixMode(GL_PROJECTION);
		glPushMatrix();
		glLoadIdentity();
		gluOrtho2D(0.0, (GLfloat) viewport[2], 0.0f, (GLfloat) viewport[3]);
		if(_startX < 0)
			_X = viewport[2] + _startX;
		if(_startY < 0)
			_Y = viewport[3] + _startY;
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		glLoadIdentity();

	}


	void UnSetOrtho()
	{
		glMatrixMode(GL_PROJECTION);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);
		glPopMatrix();
	}


public:
	FlatObject(GLubyte *bytes, int startX, int startY, int width, int height, GLubyte *picBytes = NULL) : Renderable()
	{
		_X = _startX = startX;
		_Y = _startY = startY;
		_width = width;
		_height = height;
		_bytes = bytes;
		_picBytes = picBytes;
		_selected = true;
	}

	/* override */
	void Render()
	{
		SetOrtho();
		glDisable(GL_LIGHTING);
		glDisable(GL_DEPTH_TEST);		

		RenderFlat();
		glEnable(GL_LIGHTING);
		glEnable(GL_DEPTH_TEST);
		UnSetOrtho();
	}

	/* override */
	void PicRender()
	{
		SetOrtho();
		glDisable(GL_LIGHTING);
		glDisable(GL_DEPTH_TEST);		
		PicRenderFlat();
		glEnable(GL_LIGHTING);
		glEnable(GL_DEPTH_TEST);
		UnSetOrtho();
	}
	
	virtual void RenderFlat()
	{
		glColor4f(1, 1, 1, 0.5);
		glRasterPos2i(_X, _Y);
		glDrawPixels(_width, _height, GL_RGB, GL_UNSIGNED_BYTE, _bytes);
	}

	virtual void PicRenderFlat()
	{
		if(_picBytes != NULL){
			glRasterPos2i(_X, _Y);
			glDrawPixels(_width, _height, GL_RGB, GL_UNSIGNED_BYTE, _picBytes);
		}

	}
};

#endif